package com.example.baseappstore.ui.adapter;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.baseappstore.BuildConfig;
import com.example.baseappstore.databinding.RvItemBinding;
import com.example.baseappstore.domain.response.RecommendBean;
import com.example.baseappstore.utils.NotificationHelper;
import com.example.baseappstore.utils.net.OkHttpUtils;
import com.example.baseappstore.utils.net.OkResult;

import java.io.File;
import java.util.List;

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.MyViewHolder> {

    private List<RecommendBean> list;

    private NotificationHelper helper;

    public RecommendAdapter(List<RecommendBean> list) {
        this.list = list;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        this.helper = new NotificationHelper(parent.getContext());
        RvItemBinding binding = RvItemBinding.inflate(layoutInflater, parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        RecommendBean data = list.get(position);
        holder.binding.tvTitle.setText(data.getApkName());
        holder.binding.tvIntro.setText(data.getIntroduce());
        Glide.with(holder.itemView).load(OkHttpUtils.BASE_URL + "/dev-api" + data.getLogoPic()).into(
                holder.binding.imageView
        );
        String apkPath = Environment.getExternalStorageDirectory().getPath()
                + File.separator + "Download" + File.separator + data.getApkName() + ".apk";
        File downLoadFile = new File(apkPath);
        if (downLoadFile.exists()) {
            downLoadFile.delete();
        }
        int mb = ((int) (Float.parseFloat(data.getApkSize().split("MB")[0]) * 100));
        holder.binding.button.setOnClickListener(v -> {
            helper.createNotificationChannel();
            helper.sendNotification("应用下载", "wew", mb, 0);
            OkHttpUtils.downloadFile(OkHttpUtils.BASE_URL + "/dev-api" + data.getDownloadPath(), downLoadFile, new OkResult() {
                @Override
                public void onSuccess(String data) {
                    float progressF = Float.parseFloat(data);
                    int progress = ((int) (progressF * 100));
                    if (helper != null) {
                        helper.updateProgress(progress);
                    }
                    if (progress == mb) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        Uri apkUri = FileProvider.getUriForFile(holder.itemView.getContext(), BuildConfig.APPLICATION_ID + ".provider", downLoadFile);
                        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        holder.itemView.getContext().startActivity(intent);
                    }
                }
            });
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    protected class MyViewHolder extends RecyclerView.ViewHolder {
        final RvItemBinding binding;

        public MyViewHolder(RvItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }


}
